/*
 ============================================================================
 Name		: ButtonControl.cpp
 Author	  : William Zhang
 Version	 : 1.0
 Copyright   : all rights reserved.
 Description : CButtonControl implementation
 ============================================================================
 */
#include <touchfeedback.h>
#include "ButtonControl.h"

CButtonControl::CButtonControl():
	iState(ENone),iButtonIcon(NULL),iButtonIconMask(NULL)
	{
	}

CButtonControl::~CButtonControl()
	{
	if(iButtonIcon)
		{
		delete iButtonIcon;
		delete iButtonIconMask;
		iButtonIcon = NULL;
		iButtonIconMask = NULL;
		}
	
	}

CButtonControl* CButtonControl::NewLC(const TRect& aRect,const CCoeControl* aParent)
	{
	CButtonControl* self = new (ELeave) CButtonControl();
	CleanupStack::PushL(self);
	self->ConstructL(aRect,aParent);
	return self;
	}

CButtonControl* CButtonControl::NewL(const TRect& aRect,const CCoeControl* aParent)
	{
	CButtonControl* self = CButtonControl::NewLC(aRect,aParent);
	CleanupStack::Pop(); // self;
	return self;
	}

void CButtonControl::ConstructL(const TRect& aRect,const CCoeControl* aParent)
	{
	SetContainerWindowL(*aParent);

	InitComponentArrayL();
	
	EnableDragEvents();
	
	SetRect(aRect);
	}


void CButtonControl::SetButtonIcon(const CFbsBitmap* aIcon,const CFbsBitmap* aIconMask)
	{
	iButtonIcon = const_cast<CFbsBitmap*>(aIcon);
	iButtonIconMask = const_cast<CFbsBitmap*>(aIconMask);
	}


void CButtonControl::Draw(const TRect&  aRect)const
	{
	CWindowGc& gc = SystemGc();
//	gc.SetBrushColor(KRgbGray);
//	gc.Clear(Rect());
	
//	gc.SetBrushStyle (CGraphicsContext::ENullBrush);
	
	switch(iState)
		{
		case ENone:
			break;
			
		case EButtonClicked:
			gc.SetBrushColor(KRgbGray);
			gc.Clear(Rect());
			break;
			
		case EButtonPressing:
			
			break;
			
		default:
			break;
		}
	
	gc.BitBltMasked(aRect.iTl,iButtonIcon,aRect.Size(),iButtonIconMask,EFalse);		
	
	}

void CButtonControl::SizeChanged()
	{
	DrawNow();
	}

void CButtonControl::HandlePointerEventL(const TPointerEvent& aPointerEvent)
	{
	switch(aPointerEvent.iType)
		{
		case TPointerEvent::EButton1Down:
			{
			iState = EButtonClicked;
	        MTouchFeedback* feedback = MTouchFeedback::Instance();
	        if ( feedback )
	            {
	            feedback->InstantFeedback( this, ETouchFeedbackSensitive );
	            }
			DrawNow();
			}
			break;
			
//		case TPointerEvent::EDrag:
//			{
//						
//			}
//			break;
//			
		case TPointerEvent::EButton1Up:
			{
			iState = ENone;
			DrawNow();
			}
			break;
			
		default:
			break;
		}
	CCoeControl::HandlePointerEventL( aPointerEvent );
	}
